﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Dapper;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Model.Result;
using Estsh.Core.Repositories;
using System.Collections;
using System.Data;
using System.Text;
using System.Security.Cryptography;
using System.Text.Json;
using Estsh.Core.Models;


namespace Estsh.Core.Wms.Repositories
{
    public class LoginRepository : BaseRepository<BaseEntity>, ILoginRepository
    {
        public LoginRepository(DapperDbContext _dapperDbContext) : base(_dapperDbContext)
        {
        }

        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="userNo">用户账号</param>
        /// <param name="userPwd">密码</param>
        /// <param name="factoryId">工厂ID</param>
        /// <param name="factoryName">工厂名称</param>
        /// <returns></returns>
        public List<SysEmp> RunLogin(string userNo, string userPwd, string factoryId, string factoryName)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                string strSql = "SELECT * FROM sys_emp (nolock) WHERE emp_no=@UserNo AND passwd=@Password AND factory_id=@factoryId AND enabled='Y'";

                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@UserNo", userNo);
                parameters.Add("@Password", MD5(userPwd));
                parameters.Add("@factoryId", factoryId);

                return dbConn.Query<SysEmp>(strSql, parameters).ToList();
            }

        }
        private string MD5(string number)
        {
            ASCIIEncoding ASCIIenc = new ASCIIEncoding();
            string strReturn = "";
            byte[] ByteSourceText = ASCIIenc.GetBytes(number);
            MD5CryptoServiceProvider Md5Hash = new MD5CryptoServiceProvider();
            byte[] ByteHash = Md5Hash.ComputeHash(ByteSourceText);
            foreach (byte b in ByteHash)
            {
                strReturn += b.ToString("x2");
            }
            return strReturn;
        }

        /// <summary>
        /// 获取工厂信息
        /// </summary>
        /// <returns></returns>
        public List<SysFactory> GetFactoryInfo()
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                string strSql = "SELECT factory_id, factory_name FROM sys_factory (nolock) WHERE enabled = 'Y'";

                return dbConn.Query<SysFactory>(strSql).ToList();

            }
        }

        /// <summary>
        /// 获取工厂信息
        /// </summary>
        /// <returns></returns>
        public List<SysFactory> GetFactoryInfo(string factory_id)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                string strSql = "SELECT * FROM sys_factory (nolock) WHERE enabled = 'Y' and factory_id=@factory_id";

                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@factory_id", factory_id);

                return dbConn.Query<SysFactory>(strSql, parameters).ToList();

            }
        }

        /// <summary>
        /// 通过用户账号获取用户ID与用户名称
        /// </summary>
        /// <param name="userNo">用户账号</param>
        /// <returns></returns>
        public List<SysEmp> GetUserInfo(string userNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                string strSql = "SELECT TOP 1 emp_id,emp_name FROM sys_emp (nolock) WHERE emp_no=@empNo";

                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@empNo", userNo);

                return dbConn.Query<SysEmp>(strSql, parameters).ToList();

            }
        }

        public List<SysEmp> GetRoleName(string loginId)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                string strSql = @" select a.*,b.role_id,c.role_name,c.role_desc from sys_emp as a (nolock) 
                                    left join sys_role_emp as b (nolock) on a.emp_id = b.emp_id
                                    left join sys_role as c (nolock) on b.role_id = c.role_id
                                    where a.emp_id = @loginId";

                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@loginId", loginId);

                return dbConn.Query<SysEmp>(strSql, parameters).ToList();

            }
        }

        /// <summary>
        /// 通过用户账号获取用户ID菜单权限
        /// </summary>
        /// <param name="userID"></param>
        /// <returns>返回结果用动态对象接收数据</returns>
        public List<GetMenu> GetRole(string userID)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                string strSql = @"select a.name ,e.emp_id,a.url dll_name,a.sort_num,a.menu_type_name,a.menu_sort_num,B.fun_name,a.icon_url  from dbo.sys_web_menu as A (nolock) 
                            inner join dbo.sys_role_privliege as B (nolock)  on a.menu_id=b.fun_name
                            inner join sys_role_emp as D (nolock)  on d.role_id=b.role_id
                            inner join sys_emp as E (nolock)  on e.emp_id=d.emp_id
                             where a.menu_type='PDA'  and e.emp_id= @userID  order by a.sort_num,a.menu_sort_num";

                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@userID", userID);

                List<GetMenu> RoleList = dbConn.Query<GetMenu>(strSql, parameters).ToList();

                return RoleList;

            }
        }
    }
}
